<?php

/**
 * This is the model class for table "adminhtml_Node".
 *
 * The followings are the available columns in table 'adminhtml_Node':
 * @property integer $node_id
 * @property integer $p_id
 * @property string $node_code
 * @property string $node_name
 * @property string $node_url
 * @property integer $sort
 * @property integer $status
 */
class AdminhtmlNode extends AdminhtmlCAR
{
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return AdminhtmlNode the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
	
	public function getResourceName()
	{
		return '后台节点';
	}
	
	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'adminhtml_node';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('p_id, sort, status', 'numerical', 'integerOnly'=>true),
			array('controller', 'length', 'max'=>100),
			array('node_code', 'length', 'max'=>100),
			array('node_name', 'length', 'max'=>45),
			array('node_url', 'length', 'max'=>255),
			array('controller, node_code, node_url', 'unique'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('node_id, p_id, node_code, node_name, node_url, sort, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'node_id' => 'ID',
			'p_id' => '上级节点',
			'controller' => '控制器',
			'node_code' => '节点标识',
			'node_name' => '节点名称',
			'node_url' => '节点URL',
			'sort' => '排序',
			'status' => '状态',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('node_id',$this->node_id);
		$criteria->compare('p_id',$this->p_id);
		$criteria->compare('node_code',$this->node_code,true);
		$criteria->compare('node_name',$this->node_name,true);
		$criteria->compare('node_url',$this->node_url,true);
		$criteria->compare('sort',$this->sort);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
			'sort'=>array('defaultOrder'=>'node_id DESC'),
			'pagination'=>array('pageSize'=>20),
		));
	}
	
	/**
	 * get node tree array to build menu HTML
	 * 
	 * @param integer $level
	 * @param integer $pid
	 */
	public function getNodeTree($level=false, $pid=0)
	{
		//MENU paramters: url,label,icon,disabled,visible,
		//sub-paramters->url: router,link,params,htmlOptions
		if ($level>=0 || $level===false) {
			$result= $this->findAllByAttributes(array('p_id'=>$pid, 'status'=>1) );
			$data= array();
			if (count($result)>0) {
				foreach ($result as $v) {
					//print_r($v->getAttribute('node_url') );
					//additionly, you can fill icon to item, or defined some item's url paramters...
					$tmp= array('url'=> array('route'=> $v->getAttribute('node_url')), 
							                  'label'=> $v->getAttribute('node_name'),
											  //'params'=> $v->getAttribute('node_url_params'),
								//'icon'=> $v->getAttribute('node_icon'),
					);
					//build next level paramters
					$_level = $level===false? $level: $level-1;
					$_pid = $v->getAttribute('node_id');
				    $children = $this->getNodeTree($_level, $_pid);
				    if (count($children)>0) {
				    	foreach ($children as $sv) {
					    	array_push($tmp, $sv);
					    }
				    }
				    //not add menu when have no children on level 0
				    if ($pid===0 && count($children)==0) {
				    	continue;
				    } else {
				    	$data[$v->getAttribute('node_id')]= $tmp;
				    }
				}
				return $data;
				
			} else {
				return ;
			}
		} else {
			return ;
		}
	}
	
	public function getNodeOptionsHash($level=false, $pid=0, $hideNotActive=false, $margin=0)
	{
		if ($level>=0 || $level===false) {
			$filer= array('p_id'=>$pid);
			if ($hideNotActive===true) {
				$filer = array_merge($filer, array('status'=>1 ) );
			}
			
			$result= $this->findAllByAttributes($filer );
			if (count($result)>0) {
				$options= array();
				foreach ($result as $v) {
					$key= (int) $v->getAttribute('node_id');
					$options[$key] = str_repeat('|---', (int) $margin ). $v->getAttribute('node_name');
					$_level = $level===false? $level: $level-1;
					$_pid = $v->getAttribute('node_id');
					$tmp = $this->getNodeOptionsHash($_level, $_pid, $hideNotActive, $margin+1);
					if ( count($tmp) >0 ) {
						$options= $options+ $tmp;
					}
				}
				return $options;
			}
			else {
				return array();
			}
		} else {
			return array();
		}
	}

	public static function getExceptionControllers()
	{
		return array('DefaultController','PrivilegeController' );
	}

	public function getRemainControllers($currentController=null)
	{
		$exceptControllers = array();
		$collection= $this->findAll();
		foreach ($collection as $k=> $v) {
			if ($v->getAttribute('controller')== $currentController )
				continue;
			if ($v->getAttribute('controller'))
				$exceptControllers[] =$v->getAttribute('controller');
		}
		return array_merge($exceptControllers, self::getExceptionControllers());
	}
	
	
}